﻿using AutoMapper;
using Common;
using Common.Dtos;
using Common.LayPage;
using Sigma.Admin.API.Context.Entity;
using Sigma.Admin.API.Context.UnitOfWork;

namespace Sigma.Admin.API.Services
{
    public class MenuService : IMenuService
    {
        private readonly IUnitOfWork _work;
        private readonly IMapper _mapper;


        public MenuService(IUnitOfWork work, IMapper mapper)
        {
            _work = work;
            _mapper = mapper;
        }
        public Task<ApiResponse<List<MenuDto>>> GetAllAsync()
        {
            try
            {
                var respo = _work.GetRepository<MenuEntity>();
                var res = respo.GetAll().ToList();
                if (res.Count == 0 || res is null)
                    return Task.FromResult(new ApiResponse<List<MenuDto>>("无数据"));
                var dtos = new List<MenuDto>();
                res.ForEach(x =>
                {
                    var dto = _mapper.Map<MenuDto>(x);
                    if (dto is not null)
                    {
                        dto.IsPermission = false;
                        dto.IsView = x.Level ==1? true: false;
                        dtos.Add(dto);
                    }

                });

                return Task.FromResult(new ApiResponse<List<MenuDto>>(null, true, dtos));
            }
            catch(Exception e)
            {
                return Task.FromResult(new ApiResponse<List<MenuDto>>($"操作失败：{e.Message}"));
            }
        }
        public Task<ApiResponse> AddAsync(MenuDto model)
        {
            try
            {
                var menu = _mapper.Map<MenuEntity>(model);
                var respo = _work.GetRepository<MenuEntity>();
                menu.CreateTime = DateTime.Now;
                menu.UpdateTime = DateTime.Now;
                respo.Insert(menu);
                var res =_work.SaveChanges();
                if (res > 0)
                {
                    return Task.FromResult(new ApiResponse("操作成功",true));
                }

                return Task.FromResult(new ApiResponse("操作失败"));
            }
            catch(Exception e)
            {
                return Task.FromResult(new ApiResponse($"操作失败：{e.Message}"));
            }
        }

        public Task<ApiResponse> DeleteAsync(int id)
            {
            try
            {
                var respo = _work.GetRepository<MenuEntity>();
                var entity = respo.GetFirstOrDefault(predicate: x => x.Id.Equals(id));
                respo.Delete(entity);
                var res = _work.SaveChanges();
                if (res > 0)
                {
                    return Task.FromResult(new ApiResponse("操作成功！", true));
                }

            }
            catch (Exception e)
            {
                return Task.FromResult(new ApiResponse($"操作失败：{e.Message}"));
            }
            return Task.FromResult(new ApiResponse("操作失败"));
        }

        public Task<ApiResponse> DeleteAsync(MenuDto menu)
        {
            try
            {
                var respo =_work.GetRepository<MenuEntity>();
                var entity = respo.GetFirstOrDefault(predicate:x=>x.Id.Equals(menu.Id));
                respo.Delete(entity);
                var res = _work.SaveChanges();
                if (res > 0)
                {
                    return Task.FromResult(new ApiResponse("操作成功！",true));
                }

            }catch(Exception e)
            {
                return Task.FromResult(new ApiResponse($"操作失败：{e.Message}"));
            }
            return Task.FromResult(new ApiResponse("操作失败"));
        }

        public Task<ApiResponse> GetAllAsync(QueryParameter query)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        

        public Task<ApiResponse> GetSingleAsync(int id)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> UpdateAsync(List<MenuDto> model)
        {
            return Task.FromResult(new ApiResponse("该功能尚在开发中..."));
        }

        public Task<ApiResponse> DeleteAsync(List<MenuDto> model)
        {
            throw new NotImplementedException();
        }
    }
}
